﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QlrrPrintService.Common.Command;
using Newtonsoft.Json;

namespace QlrrPrintService.Common
{
    public class PrintTemplateManager
    {
        #region fileds

        private System.Threading.ReaderWriterLockSlim _lock = new System.Threading.ReaderWriterLockSlim();

        private Dictionary<string, PrintPaperTemplate> _dic = new Dictionary<string, PrintPaperTemplate>();

        public static PrintTemplateManager Instance = new PrintTemplateManager();

        #endregion

        #region constructors

        private PrintTemplateManager() { }

        #endregion

        public int AddPaperTemplate(string json)
        {
            if (string.IsNullOrEmpty(json))
                throw new ArgumentException();
            try
            {
                var template = JsonConvert.DeserializeObject<PrintPaperTemplate>(json);
                if (template.Id == null)
                {
                    throw new ArgumentException("id不能为空");
                }
                return AddPaperTemplate(template.Id, template);
            }
            catch
            {
                throw new ArgumentException();
            }
        }

        public void ResetPaperTemplate(string json)
        {
            if (string.IsNullOrEmpty(json))
                return;
            try
            {
                var template = JsonConvert.DeserializeObject<PrintPaperTemplate>(json);
                if (template.Id == null)
                {
                    throw new ArgumentException("id不能为空");
                }
                ResetPaperTemplate(template.Id, template);
            }
            catch
            {
                throw new ArgumentException();
            }
        }

        

        public int RemovePrintTemplate(string json)
        {
            if (string.IsNullOrEmpty(json))
                return -1;
            string id = "";
            try
            {
                var message = JsonConvert.DeserializeObject<PrintPaperTemplate>(json);
                id = message.Id;
            }
            catch
            {
                return -1;
            }
            
            _lock.EnterWriteLock();
            try
            {
                if (!string.IsNullOrEmpty(id))
                {
                    _dic.Remove(id);
                }
                
            }
            finally { _lock.ExitWriteLock(); }
            return 0;
        }

        public void Clean()
        {
            
            _lock.EnterWriteLock();
            try
            {
                _dic.Clear();
            }
            finally { _lock.ExitWriteLock(); }
            
        }

        public PrintPaperTemplate GetPrintPaperTemplate(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            if (_dic.ContainsKey(id))
            {
                _lock.EnterReadLock();
                try
                {
                    return _dic[id];
                }
                finally
                { _lock.ExitReadLock(); }
            }
            else
                return null;
            
        }

        public int AddPaperTemplate(string id, PrintPaperTemplate template)
        {
            if (!string.IsNullOrEmpty(id) && template != null)
            {
                _lock.EnterWriteLock();
                try
                {
                    if (!_dic.ContainsKey(id))
                    {
                        _dic.Add(id, template);
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                finally { _lock.ExitWriteLock(); }

            }
            else
            {
                return 2;
            }
           
        }

        public void ResetPaperTemplate(string id, PrintPaperTemplate template)
        {
            if (!string.IsNullOrEmpty(id) && template != null)
            {
                _lock.EnterWriteLock();
                try
                {
                   _dic[id] = template;
                }
                finally { _lock.ExitWriteLock(); }
            }
        }
    }
}
